package com.ruoyi.project.determination.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.determination.pojo.*;
import com.ruoyi.project.determination.service.DataAssignmentService;
import com.ruoyi.project.fusion.domain.PriceScores;
import com.ruoyi.project.fusion.service.DataFusionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/rest/data/management/enterprise")
public class DataAssignmentController {
    @Autowired
    private DataFusionService dataFusionService;

    @Autowired
    private DataAssignmentService dataAssignmentService;

    @Log(title = "智能采购决策优化构件", businessType = BusinessType.OTHER)
    @PostMapping("/product/assignment/query")
    public List<OptimizedAssignmentResults> getAssignmentResults(String chainType, String scenario, @RequestBody AssignmentParameters parameters) {
        List<PriceScores> priceScores = dataFusionService.getPriceScores(chainType, scenario);
        return dataAssignmentService.getAssignmentResults(parameters, priceScores);
    }

    @GetMapping("/scope/query")
    public List<String> getEnterprises(String chainType, String scenario, String product) {
        List<PriceScores> priceScores = dataFusionService.getPriceScores(chainType, scenario);

        // 使用 Stream API 过滤符合指定 product 的 PriceScores，并收集企业名称
        return priceScores.stream()
                .filter(score -> product.equals(score.getProduct())) // 过滤出产品匹配的条目
                .map(PriceScores::getEnterpriseName)                 // 提取企业名称
                .distinct()                                          // 去重
                .collect(Collectors.toList());                       // 收集结果到列表
    }


//    @GetMapping("/product/assignment/query2")
//    public List<OptimizedAssignmentResults> getAssignmentResults(String chainType, String scenario) {
//        List<PriceScores> priceScores = dataFusionService.getPriceScores(chainType, scenario);
//        AssignmentParameters parameters = new AssignmentParameters();
//        Map<String, List<Integer>> productDemands = new HashMap<>();
//        productDemands.put("商业洗衣机", Arrays.asList(15000, 16000, 20000, 20000, 20000, 20000));
//        productDemands.put("全自动洗衣机", Arrays.asList(20000, 20000, 20000, 20000, 20000, 20000));
//        parameters.setProductDemands(productDemands);
//
//        Map<String, Map<String, List<Integer>>> corporationLimits = new HashMap<>();
//        Map<String, List<Integer>> corporationLimit = new HashMap<>();
//        corporationLimit.put("酒钢集团", Arrays.asList(1500, 1600, 1400, 1700, 1500, 1600));
//        corporationLimits.put("商业洗衣机", corporationLimit);
//        parameters.setCorporationLimits(corporationLimits);
//
//        return dataAssignmentService.getAssignmentResults(parameters, priceScores);
//    }
}
